<?php
/**
 * 登陆操作
 **/
namespace Xijie\Controller;
use Think\Controller;

class WapController extends Controller {
	/**
	 * 用户登录操作
	 **/
	public function pay(){
		if(IS_GET){
			$this->display('index');
		}else if(IS_POST){
			
		}
	}
	
	/**
	 * 自助制卡
	 *
	 **/
	public function index(){
		if(IS_GET){
			$this->display('select');
		}else if(IS_POST){
			$phone = I('post.phone') ? trim(I('post.phone')) : '';
			$code = I('post.code') ? trim(I('post.code')) : '';
			$type = I('post.type') ? (int)I('post.type') : 1;
			//用户信息
			$info = D('Member')->getMemberInfoByPhone($phone, $code);
			if(is_array($info) && count($info)){
				if($info['leaving_time'] < time()){
					$this->ajaxReturn(array(
						'errCode' => 0,
						'errMsg' => '您的租期已失效'
					));
				}else{
					if($type == 1){
						$msg = "尊敬的 ". $info['room_number'] ." 房间 ". $info['member_nick'] ." 客人，您好！<br />
						你的租期是：". date('Y-m-d H:i', $info['checkin_time'])."~". date('Y-m-d H:i', $info['leaving_time']) ."<br />
						查询不到你的信息，请与管理员确认！";
						$this->ajaxReturn(array(
							'errCode' => 1,
							'errMsg' => $msg,
							'info' => $info,
						));
					}else{
						$dw = new \COM("DynamicWrapper");
						$dw->Register('proRFL.dll', 'GuestCard', 'i=llllllssss', 'r=l');
						//设置参数
						$btime = date('ymdHi', $info['checkin_time']);
						$etime = date('ymdHi', $info['leaving_time']);
						//设置锁号
						$locknum = str_pad($info['room_number'], 6, '01', STR_PAD_LEFT).'99';
						//开始制卡
						$ch = $dw->GuestCard(1,3473408,0,0,1,0, $btime, $etime, $locknum,'1');
						//echo $btime . ' ' . $etime . ' '. $locknum. $ch; 
						if(!$ch){
							//保存数据到数据库
							D('Roomcard')->addRoomCardnfo(array(
								'card' => $locknum,
								'card_type' => 6,
								'lock_id' => $locknum,
								'member_id' => $info['member_id'],
								'room_number' => $info['room_number'],
								'createtime' => time(),
								'checkin_time' => $info['checkin_time'],
								'leav_time' => $info['leaving_time']
							));
							
							//鸣叫一次
							$dw->Register('proRFL.dll', 'Buzzer', 'i=ll', 'r=l');
							$ch = $dw->Buzzer(1, 10);
							if(!$ch){
								$this->ajaxReturn(array(
									'errCode' => 1,
									'errMsg' => '自助制卡成功'
								));
							}
						}else{
							$this->ajaxReturn(array(
								'errCode' => 0,
								'errMsg' => '自助制卡失败'
							));
						}
					}
				}
			}else{
				$this->ajaxReturn(array(
					'errCode' => 0,
					'errMsg' => '未查询到相关数据'
				));
			}	
		}
	}
	
	//userinfo
	public function ajax(){
		$phone = I('get.phone') ? trim(I('get.phone')) : '';
		$code = I('get.code') ? trim(I('get.code')) : '';
		$type = I('get.type') ? (int)I('get.type') : 1;
		
		//用户信息
		$info = D('Member')->getMemberInfoByPhone($phone, $code);
		if(is_array($info) && count($info)){
			if($info['leaving_time'] < time()){
				$this->ajaxReturn(array(
					'errCode' => 0,
					'errMsg' => '您的租期已失效'
				));
			}else{
				if($type == 1){
					$msg = "尊敬的 ". $info['room_number'] ." 房间 ". $info['member_nick'] ." 客人，您好！<br />
					你的租期是：". date('Y-m-d H:i', $info['checkin_time'])."~". date('Y-m-d H:i', $info['leaving_time']) ."<br />
					";
					//设置参数
					if($info['checkin_time'] >= time()){
						$info['btime'] = date('ymdHi', $info['checkin_time']);
					}else{
						$info['btime'] = date('ymdHi', time());
					}
					
					$info['etime'] = date('ymdHi', $info['leaving_time']);
					//设置锁号
					$info['locknum'] = str_pad($info['room_number'], 6, '01', STR_PAD_LEFT).'99';
					
					$this->ajaxReturn(array(
						'errCode' => 1,
						'errMsg' => $msg,
						'info' => $info,
					));
				}else{
					//
				}
			}
		}else{
			$this->ajaxReturn(array(
				'errCode' => 0,
				'errMsg' => '查询不到您的信息，请与管理员确认！'
			));
		}	
	}
	
	//card log
	public function card(){
		$data['card'] = I('get.card') ? trim(I('get.card')) : '';
		$data['card_udid'] = $data['card'];
		$data['card_type'] = 6;
		$data['lock_id'] = I('get.lock_id') ? trim(I('get.lock_id')) : '';
		$data['member_id'] = I('get.uid') ? (int)I('get.uid') : 0;
		$data['createtime'] = time();
		
		$userinfo = D('Member')->getMemberInfoById($data['member_id']);
		
		if(is_array($userinfo) && count($userinfo)){
			$data['checkin_time'] = $userinfo['checkin_time'];
			$data['leav_time'] = $userinfo['leaving_time'];
			$data['room_number'] = $userinfo['room_number'];
			$data['m_nickname'] = $userinfo['member_nick'];
			
			$row = D('Roomcard')->addRoomCardnfo($data);
			if($row){
				$this->ajaxReturn(array(
					'errCode' => 1,
					'errMsg' => 'Success.',
				));
			}
		}else{
			$this->ajaxReturn(array(
				'errCode' => 0,
				'errMsg' => 'Fail!',
			));
		}
	}
}

